#!/bin/bash

sshFlag=0
if [ ! -s /home/hadoop/.ssh/id_rsa ]; then
  # ssh-keygen
  expect -c "
  spawn ssh-keygen
    expect {
      \"*y/n*\" {send \"y\r\"; exp_continue}
      \"*key*\" {send \"\r\"; exp_continue}
      \"*passphrase*\" {send \"\r\"; exp_continue}
      \"*again*\" {send \"\r\";}
    }
  "
  sshFlag=1
fi

if [ $sshFlag -eq  1 ];then
  # ssh-copy-id
  echo $sshFlag;
  password="123456"
  expect -c "
  spawn ssh-copy-id   hadoop@node01
    expect {
      \"*yes/no*\" {send \"yes\r\"; exp_continue}
      \"*password*\" {send \"$password\r\"; exp_continue}
      \"*Password*\" {send \"$password\r\";}
    }
  "
fi